//
//  HomeController.swift
//  WeiBo
//
//  Created by 江潘 on 15/6/24.
//  Copyright © 2015年 江潘. All rights reserved.
//

import UIKit
import SVProgressHUD

class HomeController: BaseController {
    
    // 行高缓存
  lazy var CellHeightCache:[Int:CGFloat] = [Int:CGFloat]()
    
 private var statuslist:[Status]? {
    didSet{
        
        tableView.reloadData()
    }
}
    override func viewDidLoad() {
        super.viewDidLoad()

        visitV?.setupVisitInfo(true, iconName: "visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜")
        
        visitV?.startAnimation()
     
        if sharedAccount != nil {
            
            setupHome()
            
            loadWebData()
            
            tableView.registerClass(HomeStatusCell.self, forCellReuseIdentifier: "cell")
        
            tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        }
        
    }
    
    private func loadWebData(){
        
        Status.loadStatus { (result, error) -> () in
            if error != nil {
                SVProgressHUD.showErrorWithStatus("你的网络不给力。。")
            } else if result == nil{
                SVProgressHUD.showWithStatus("您没有新的微博刷新。。。")
            } else{
                
                self.statuslist = result
                
                print(self.statuslist)
                
            }
        }
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.statuslist?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
      let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! HomeStatusCell
        
      print(self.statuslist![indexPath.row])
        
      cell.status = self.statuslist![indexPath.row]
        
      return cell
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        if CellHeightCache[(self.statuslist![indexPath.row].id)] != nil {
            
            return CellHeightCache[self.statuslist![indexPath.row].id]!
        }
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! HomeStatusCell
        
        let h = cell.rowHeight(self.statuslist![indexPath.row])
        
        CellHeightCache[self.statuslist![indexPath.row].id] = h
        
        return h
    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
    
        visitV?.startAnimation()
    }
    
    let popAnimation = PopAnimation()
    
    func clickBtn(){
    
        let sb = UIStoryboard(name: "popView", bundle: nil)
        
        let vc = sb.instantiateViewControllerWithIdentifier("pop")
        
        // 设置转场代理
        vc.transitioningDelegate = popAnimation
        // 自定义转场动画
        vc.modalPresentationStyle = UIModalPresentationStyle.Custom
        
        let x = (view.bounds.width - 200) * 0.5
        
        popAnimation.presentedFrame = CGRectMake(x, 56, 200, 300)
        
        presentViewController(vc, animated: true, completion: nil)
        
    }
    
    private func setupHome(){
        
       let leftBtn = UIButton()
        
       leftBtn.frame = CGRectMake(0, 0, 30, 30)
        
       leftBtn.setImage(UIImage(named: "navigationbar_friendsearch"), forState: UIControlState.Normal)
    
       leftBtn.setImage(UIImage(named: "navigationbar_friendsearch_highlighted"), forState: UIControlState.Highlighted)
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView:leftBtn)
        
        let rightBtn = UIButton()
        
        rightBtn.frame = CGRectMake(0, 0, 30, 30)
        
        rightBtn.setImage(UIImage(named: "navigationbar_pop"), forState: UIControlState.Normal)
        
        rightBtn.setImage(UIImage(named: "navigationbar_pop_highlighted"), forState: UIControlState.Highlighted)
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView:rightBtn)

        let name = sharedAccount?.name!
        
        let titleBtn = HomeButton()
        
        titleBtn.setTitle(name!+" ", forState: UIControlState.Normal)
        
        titleBtn.frame = CGRectMake(0, 0, 120, 30)
        
        titleBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        titleBtn.titleLabel?.font = UIFont.systemFontOfSize(17.0)
        
        titleBtn.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        
        navigationItem.titleView = titleBtn
        
        titleBtn.addTarget(self, action: "clickBtn", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
}


